package agency.tango.materialintroscreen.listeners;

import java.util.ArrayList;
import java.util.List;

import ohos.agp.components.PageSlider;

import agency.tango.materialintroscreen.adapter.SlidesAdapter;
import agency.tango.materialintroscreen.animations.ViewTranslationWrapper;

/**
 * @ProjectName: material-intro-screen-master
 * @Package: agency.tango.materialintroscreen.listeners
 * @ClassName: ViewBehavioursOnPageChangeListener
 * @Description: 随着pageslider页面变化 View的Behaviour随之变化的监听器的实现类
 * @CreateDate: 2021/4/12 16:47
 * @UpdateDate: 2021/4/12 16:47
 * @UpdateRemark: 暂无
 * @Version: 1.0
 */
public class ViewBehavioursOnPageChangeListener implements PageSlider.PageChangedListener {
    private SlidesAdapter adapter;

    private final List<IPageSelectedListener> listeners = new ArrayList<>();
    private final List<ViewTranslationWrapper> wrappers = new ArrayList<>();
    private final List<IPageScrolledListener> pageScrolledListeners =
            new ArrayList<>();

    public ViewBehavioursOnPageChangeListener(SlidesAdapter adapter) {
        this.adapter = adapter;
    }

    /**
     * 注册页面选择监听器
     *
     * @param pageSelectedListener 页面选择监听器
     * @return ViewBehaviours页面滑动监听器
     */
    public ViewBehavioursOnPageChangeListener registerPageSelectedListener(IPageSelectedListener pageSelectedListener) {
        listeners.add(pageSelectedListener);
        return this;
    }

    /**
     * 注册控件位移封装
     *
     * @param wrapper 位移封装器
     * @return ViewBehaviours页面滑动监听器
     */
    public ViewBehavioursOnPageChangeListener registerViewTranslationWrapper(ViewTranslationWrapper wrapper) {
        wrappers.add(wrapper);
        return this;
    }

    /**
     * 注册页面滑动监听
     *
     * @param pageScrolledListener 滑动监听器
     * @return ViewBehaviours页面滑动监听器
     */
    public ViewBehavioursOnPageChangeListener registerOnPageScrolled(IPageScrolledListener pageScrolledListener) {
        pageScrolledListeners.add(pageScrolledListener);
        return this;
    }

    @Override
    public void onPageSliding(int position, float positionOffset, int positionOffsetPixels) {
        if (adapter == null) {
            return;
        }
        if (isFirstSlide(position)) {
            for (ViewTranslationWrapper wrapper : wrappers) {
                wrapper.enterTranslate(positionOffset);
            }
        } else if (adapter.isLastSlide(position)) {
            for (ViewTranslationWrapper wrapper : wrappers) {
                wrapper.exitTranslate(positionOffset);
            }
        } else {
            for (ViewTranslationWrapper wrapper : wrappers) {
                wrapper.defaultTranslate(positionOffset);
            }
        }

        for (IPageScrolledListener pageScrolledListener :
                pageScrolledListeners) {
            pageScrolledListener.pageScrolled(position, positionOffset);
        }
    }

    @Override
    public void onPageSlideStateChanged(int state) {
    }

    @Override
    public void onPageChosen(int position) {
        for (IPageSelectedListener pageSelectedListener : listeners) {
            pageSelectedListener.pageSelected(position);
        }
    }


    private boolean isFirstSlide(int position) {
        return position == 0;
    }
}